package main;


import processes.StartStop;
import java.util.LinkedList;
import processes.ProcPrimitives;
import processes.Process;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author Ramunas
 */
public class GUI extends javax.swing.JFrame implements Runnable{
    private boolean started = false; 
    private boolean notEmpty = true;
    /**
     * Creates new form GUI
     */
    public GUI() {
        initComponents();
        jScrollPane3.setColumnHeader(null);
        runGUI.start();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        RegistersLabel = new javax.swing.JLabel();
        PTRLabel = new javax.swing.JLabel();
        SFLabel = new javax.swing.JLabel();
        ModeLabel = new javax.swing.JLabel();
        PILabel = new javax.swing.JLabel();
        SILabel = new javax.swing.JLabel();
        TILabel = new javax.swing.JLabel();
        PTRField = new javax.swing.JTextField();
        SFField = new javax.swing.JTextField();
        ModeField = new javax.swing.JTextField();
        PIField = new javax.swing.JTextField();
        SIField = new javax.swing.JTextField();
        TIField = new javax.swing.JTextField();
        ProcessesLabel = new javax.swing.JLabel();
        ResourcesLabel = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        ProcessesTable = new javax.swing.JTable();
        jScrollPane2 = new javax.swing.JScrollPane();
        ResourcesTable = new javax.swing.JTable();
        MemoryLabel = new javax.swing.JLabel();
        jScrollPane3 = new javax.swing.JScrollPane();
        MemoryTable = new javax.swing.JTable();
        StartButton = new javax.swing.JButton();
        StepButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        RegistersLabel.setText("Registers");

        PTRLabel.setText("PTR");

        SFLabel.setText("SF");

        ModeLabel.setText("MODE");

        PILabel.setText("PI");

        SILabel.setText("SI");

        TILabel.setText("TI");

        PTRField.setText("0000");

        SFField.setText("0000");

        ModeField.setText("0");

        PIField.setText("0");

        SIField.setText("0");

        TIField.setText("0");
        TIField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                TIFieldActionPerformed(evt);
            }
        });

        ProcessesLabel.setText("Processes");

        ResourcesLabel.setText("Resources");

        ProcessesTable.setModel(procTable);
        jScrollPane1.setViewportView(ProcessesTable);

        ResourcesTable.setModel(resTable);
        jScrollPane2.setViewportView(ResourcesTable);

        MemoryLabel.setText("Real Memory");

        MemoryTable.setModel(memory);
        MemoryTable.getTableHeader().setReorderingAllowed(false);
        jScrollPane3.setViewportView(MemoryTable);

        StartButton.setText("Start");
        StartButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                StartButtonActionPerformed(evt);
            }
        });

        StepButton.setText("Step");
        StepButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                StepButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(RegistersLabel)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(TILabel)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(TIField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(ModeLabel)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(ModeField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(PTRLabel)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(PTRField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(SILabel)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(SIField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(PILabel)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(PIField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(SFLabel)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(SFField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(StartButton)
                    .addComponent(StepButton))
                .addGap(27, 27, 27)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(MemoryLabel)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(ProcessesLabel)
                        .addGap(280, 280, 280)
                        .addComponent(ResourcesLabel))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 298, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(30, 30, 30)
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 419, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(46, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(RegistersLabel)
                    .addComponent(ProcessesLabel)
                    .addComponent(ResourcesLabel))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(PTRLabel)
                            .addComponent(PTRField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(SFLabel)
                            .addComponent(SFField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(SILabel)
                            .addComponent(SIField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(PILabel)
                            .addComponent(PIField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(TILabel)
                            .addComponent(TIField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(ModeLabel)
                            .addComponent(ModeField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 129, Short.MAX_VALUE)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE))
                .addGap(28, 28, 28)
                .addComponent(MemoryLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 270, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(StartButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(StepButton)))
                .addContainerGap(46, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void TIFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_TIFieldActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_TIFieldActionPerformed

    private void StartButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_StartButtonActionPerformed
        if(!started){
            started = true;
            ProcPrimitives.createProcess(new StartStop(), null);
            while(notEmpty){
                notEmpty = ProcPrimitives.processPlanner();
            }
        }
    }//GEN-LAST:event_StartButtonActionPerformed

    private void StepButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_StepButtonActionPerformed
        if(!started){
            started = true;
            ProcPrimitives.createProcess(new StartStop(), null);
        }else{
            ProcPrimitives.processPlanner();
        }
        this.repaint();     
    }//GEN-LAST:event_StepButtonActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                new GUI().setVisible(true);
                
            }
            
        });      
    }
    private Memory memory = new Memory();
    private ProcTable procTable = new ProcTable();
    private ResTable resTable = new ResTable();
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel MemoryLabel;
    private javax.swing.JTable MemoryTable;
    private javax.swing.JTextField ModeField;
    private javax.swing.JLabel ModeLabel;
    private javax.swing.JTextField PIField;
    private javax.swing.JLabel PILabel;
    private javax.swing.JTextField PTRField;
    private javax.swing.JLabel PTRLabel;
    private javax.swing.JLabel ProcessesLabel;
    private javax.swing.JTable ProcessesTable;
    private javax.swing.JLabel RegistersLabel;
    private javax.swing.JLabel ResourcesLabel;
    private javax.swing.JTable ResourcesTable;
    private javax.swing.JTextField SFField;
    private javax.swing.JLabel SFLabel;
    private javax.swing.JTextField SIField;
    private javax.swing.JLabel SILabel;
    private javax.swing.JButton StartButton;
    private javax.swing.JButton StepButton;
    private javax.swing.JTextField TIField;
    private javax.swing.JLabel TILabel;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    // End of variables declaration//GEN-END:variables
    private Thread runGUI = new Thread(this);
    
    @Override
    public void run() {
         while(true){
            try{
                Thread.sleep(200);
            }catch(InterruptedException e){
                 System.out.println(e);       
            }
            ProcessesTable.revalidate();
            ResourcesTable.revalidate();
        }
    }
}
